package com.edgclub.edg.utils.util;

import android.media.MediaRecorder;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;

import com.edgclub.edg.Constant;

import java.io.File;
import java.io.IOException;

public class VoiceRecorderUtil {
	private static int SAMPLE_RATE_IN_HZ = 8000;

	private MediaRecorder recorder;

	private String voicePath = "voice.amr";

	private static int RECORD_NO = 0; // 不在录音
	private static int RECORD_ING = 1; // 正在录音
	private static int RECODE_ED = 2; // 完成录音

	private static int RECODE_STATE = 0; // 录音的状态

	private static float recodeTime = 0.0f; // 录音的时间
	private static double voiceValue = 0.0; // 麦克风获取的音量值

	private static int MAX_TIME = Integer.MAX_VALUE; // 最长录制时间，单位秒，0为无时间限制
	private static int MIX_TIME = 1; // 最短录制时间，单位秒，0为无时间限制，建议设为1

	private Handler imgHandle;
	
	private String errorStr;

	public VoiceRecorderUtil(Handler imgHandle) {
		super();
		this.imgHandle = imgHandle;
	}

	public void startRecording() throws IOException {

		recorder = new MediaRecorder();
		RECODE_STATE = RECORD_ING;
		deleteOldFile();
		
		recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
		recorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
		recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
		recorder.setAudioSamplingRate(SAMPLE_RATE_IN_HZ);
		recorder.setOutputFile(getFilePath());
		
		try {
			recorder.prepare();
			recorder.start();
		} catch (Exception e) {
			// TODO: handle exception
			errorStr = "录音失败，请检查是否开启录音权限";
		}
	
		mythread();
	}

	// 录音计时线程
	private void mythread() {
		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				recodeTime = 0.0f;
				while (RECODE_STATE == RECORD_ING) {
					try {
						Thread.sleep(200);
						recodeTime += 0.2;
						if(RECODE_STATE == RECORD_ING){
							voiceValue = getAmplitude();
							setImageBg();
						}
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}).start();
	}

	public String getFilePath() {
		return Constant.DIR + "/" + voicePath;
	}

	// 删除老文件
	private void deleteOldFile() {
		File file = new File(Environment.getExternalStorageDirectory(),
				voicePath);
		if (file.exists()) {
			Log.d("dddddbbbb", file.getAbsolutePath());
			file.delete();
		}
	}

	public void stopRecoding() throws IOException {
		if (recorder != null) {
			RECODE_STATE = RECODE_ED;
			try {
				//设置后不会崩
				recorder.setOnErrorListener(null);
				recorder.setPreviewDisplay(null);
				recorder.stop();
			} catch (Exception e) {
				// TODO: handle exception
			}
		
			recorder.release();
			recorder = null;
		}
	}

	public void discardRecording() {
		RECODE_STATE = RECORD_NO;
		try {
			stopRecoding();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		deleteOldFile();
	}

	public float getRecodeTime() {
		File file = new File(getFilePath());
		if (file == null || !file.exists() || !file.isFile())
			return -1011;
		if (file.length() == 0L) {
			file.delete();
			return -1011;
		}
		return  recodeTime;
//		if (recodeTime < 1) {
//			return 0;
//		} else {
//			return (int) recodeTime;
//		}
	}

	private double getAmplitude() {
		if (recorder != null) {
			return (recorder.getMaxAmplitude());
		} else
			return 0;
	}

	public String getErrorStr() {
		return errorStr;
	}

	public void setErrorStr(String errorStr) {
		this.errorStr = errorStr;
	}

	// 录音图片随声音大小切换
	private void setImageBg() {
		if (voiceValue < 200.0) {
			imgHandle.sendEmptyMessage(0);
		} else if (voiceValue > 200.0 && voiceValue < 400) {
			imgHandle.sendEmptyMessage(1);
		} else if (voiceValue > 400.0 && voiceValue < 800) {
			imgHandle.sendEmptyMessage(2);
		} else if (voiceValue > 800.0 && voiceValue < 1600) {
			imgHandle.sendEmptyMessage(3);
		} else if (voiceValue > 1600.0 && voiceValue < 3200) {
			imgHandle.sendEmptyMessage(4);
		} else if (voiceValue > 3200.0 && voiceValue < 5000) {
			imgHandle.sendEmptyMessage(5);
		} else if (voiceValue > 5000.0 && voiceValue < 7000) {
			imgHandle.sendEmptyMessage(6);
		} else if (voiceValue > 7000.0 && voiceValue < 10000.0) {
			imgHandle.sendEmptyMessage(7);
		} else if (voiceValue > 10000.0 && voiceValue < 14000.0) {
			imgHandle.sendEmptyMessage(8);
		} else if (voiceValue > 14000.0 && voiceValue < 17000.0) {
			imgHandle.sendEmptyMessage(9);
		} else if (voiceValue > 17000.0 && voiceValue < 20000.0) {
			imgHandle.sendEmptyMessage(10);
		} else if (voiceValue > 20000.0 && voiceValue < 24000.0) {
			imgHandle.sendEmptyMessage(11);
		} else if (voiceValue > 24000.0) {
			imgHandle.sendEmptyMessage(12);
		}
//		else if (voiceValue > 28000.0) {
//			imgHandle.sendEmptyMessage(13);
//		}
	}
}